/*
 * @Author: mikey.zhaopeng
 * @Date:   2016-05-26 17:32:42
 * @Last Modified by:   mikey.zhaopeng
 * @Last Modified time: 2016-07-13 16:16:39
 */

'use strict';
define([
    '../../../framework/base/baseView',
    '../../../framework/bean/ecCreativeTemplate',
    'text!../tpl/previewDialog.html'
], function(
    BaseView,
    ecCreativeTemplateModel,
    tpl
) {
    function me() {
        BaseView.apply(this, arguments);
        var self = this;
        this.init = function(callback, type, previewParams) {
            self.callback = callback; //回调
            self.type = type; //调用来源，是创建create还是编辑edit
            self.previewParams = previewParams; //当来源是edit的时候，使用
            self.$root = $('#create-material-dialog');
            self.step1Key = 'createMaterialStep1'; //step1缓存
            self.step2Key = 'createMaterialStep2'; //step2缓存
            self.generatedFeed = new Array();
            self.render();
        }

        this.render = function() {
            self.$root.empty();
            self.$root.html(I18N.parse(tpl,'createMaterial'));
            self.event();
            self.$root.modal('show');
        }

        this.event = function() {
            self.$root.off('shown.bs.modal').on('shown.bs.modal', $.proxy(self.shown, self))
            self.$root.find('.btn-ok').off('click').on('click', $.proxy(self.shown, self));
        }

        this.shown = function(e) {
            self.generateMore();
        }

        this.generateMore = function() {
            self.generatedFeed = [];
            var generateParams = {};
            if (self.type == 'create') {
                generateParams = {
                    wefeedid: self.kit.getCache(self.step1Key, 'feedId'),
                    ecTemplateId: self.kit.getCache(self.step2Key, 'ecTemplateId'),
                    mainBgColor: self.kit.getCache('createMaterialStep2', 'mainBgColor'),
                    joinBgColor: self.kit.getCache('createMaterialStep2', 'joinBgColor'),
                    more: true,
                    elements: self.kit.getCache(self.step2Key, 'elements')
                }
            } else if (self.type == 'edit') {
                generateParams = self.previewParams;
            } else {
                self.kit.alert2(self.i18n('parameterError'));
                return;
            }
            self.$root.find('.preview-thumbnail').empty();
            self.$root.find('.col-md-4').remove();
            self.$root.find('.modal-footer').addClass('hidden');

            self.$root.find('.preview-thumbnail').append('<div class="col-md-4 thumbnail-loading"><a class="thumbnail"><img style="padding-top: 45%;" src="./imgs/imgloading.gif"></a></div>');
            self.$root.find('.preview-thumbnail .thumbnail').height(self.$root.find('.preview-thumbnail .thumbnail').width());
            ecCreativeTemplateModel.buildCreative(generateParams, function(res) {
                //生成创意后 回调 发轮询请求 预览数据
                if (res && res.length > 0)
                    self.updatePreviewDiv(res);

            });
        }

        /**
         * { 定时刷新预览图片 }
         *
         * @method     updatePreviewDiv
         */
        this.updatePreviewDiv = function(jobIds) {
            var _this = this;
            var len = jobIds.length;

            $.each(jobIds, function(a, b) {
                var jobParams = {
                    jobId: b,
                    _disabledShowloading: true
                }
                ecCreativeTemplateModel.buildCreativeResult(jobParams, function(res) {
                    if (res) {
                        //预览图未生成完，继续渲染
                        $.each(res.feedList, function(i, v) {
                            if ($.inArray(v.id, self.generatedFeed) == -1) {
                                self.generatedFeed.push(v.id)
                                self.$root.find('.thumbnail-loading').before('<div class="col-md-4"><a class="thumbnail"><img src="' + v.image_link + '"></a></div>');
                            }
                        });

                        if (res.status == 2) {
                            if (res.originalFeedSum == 50)
                                self.$root.find('.modal-footer').removeClass('hidden');

                            self.$root.find('.preview-thumbnail .thumbnail-loading').remove();
                            console.log('done--' + b);
                            jobIds.splice($.inArray(b, jobIds), 1);
                            if (len-- == 0)
                                window.clearTimeout(_this.stoupdataState);
                        }
                    }
                });
            });

            //定时刷新数据
            _this.stoupdataState = window.setTimeout(function() {
                _this.updatePreviewDiv(jobIds);
            }, 5000);
        }


        this.i18n = function(key) {
            return I18N.getText('createMaterial', key)
        }

    }
    return me;
});